// pages/modify_information/index.js
import Toast from '@vant/weapp/toast/toast'
const $http = require('../../request/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    message: {
      name: "",
      phone: "",
      studentId: "",
      schoolName: "",
      departmentName: "",
      className: "",
    },

    test: "",
    Name_show: "",
    Phone_show: "",
    StudentId_show: "",
    // 学校名字的集合
    School_Name_Gather: [],
    // 所有学校名字对象的集合(对应学校名称和ID)
    School_Message_Gather: [],
    // 部门名字的集合
    Department_Name_Gather: [],
    // 所有部门名字对象的集合(对应部门名称和ID)
    Department_Message_Gather: [],
    // 班级名字的集合
    Class_Name_Gather: [],
    // 所有班级名字对象的集合(对应班级名称和ID)
    Class_Message_Gather: [],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setUserMessageAndShow(options);
    console.log(this.getSchool())
    let that = this;
    this.getSchool().then(function (data) {
      that.getDepartment(data).then(function (data) {
        that.getClass(data)
      })
    }, function (error) {

    });

  },
  onshow: function (options) {
    this.getSchool();
  },
  setUserMessageAndShow(options) {
    this.setData({
      ["message.name"]: options.name || "",
      ["message.phone"]: options.phone || "",
      ["message.studentId"]: options.studentId || "",
      ["message.schoolName"]: options.SchoolName || "",
      ["message.departmentName"]: options.DepartmentName || "",
      ["message.className"]: options.ClassName || "",
      StudentId_show: options.studentId || "",
      Phone_show: options.phone || "",
      Name_show: options.name || "",
    })
    // 回显的时候赋值
  },



  getSchool() {
    return new Promise((resolve, reject) => {
      $http.request("GET", "/front/school/all", "").then(res => {
        console.log("res", res.data.data);
        // 解构赋值 拿取学校信息(name,id,depth)
        let { data } = res.data
        let tmp = [];
        for (let i = 0; i < data.length; i++) {
          tmp.push(data[i].name)
        };
        this.setData({
          School_Message_Gather: data,
          School_Name_Gather: tmp
        })
        if (res.data.code == 200) {
          resolve(data)
        }
      })


    })
  },
  getDepartment(data) {
    return new Promise((resolve, reject) => {
      let schoolId = "";
      let app = getApp();
      // 使用foreach 筛查企业
      data.forEach(item => {
        if (item.name == app.userInfo.schoolName) {
          schoolId = item.id
        }
      })
      $http.request("GET", `/front/dept/all?schoolId=${schoolId}`, "").then(res => {
        console.log(res)
        let { data } = res.data;
        let tmp = [];
        for (let i = 0; i < data.length; i++) {
          tmp.push(data[i].name)
        };
        this.setData({
          Department_Message_Gather: data,
          Department_Name_Gather: tmp
        })
        resolve(data)
      })
    })
  },


  getClass(data) {
    return new Promise((resolve, reject) => {
      let deptId = "";
      // 使用foreach 筛查部门
      let app = getApp()
      data.forEach(item => {
        if (item.name == app.userInfo.deptName) {
          deptId = item.id
        }
      })
      $http.request("GET", `/front/class/all?deptId=${deptId}`, "").then(res => {
        let { data } = res.data;
        let tmp = [];
        for (let i = 0; i < data.length; i++) {
          tmp.push(data[i].name)
        };
        this.setData({
          Class_Message_Gather: data,
          Class_Name_Gather: tmp
        })
        resolve(data)
      })
    })
  },

  save() {
    wx.redirectTo({
      url: `/pages/message/index`,
      success: (result) => { },
    });
  },
  changeName(event) {
    var app = getApp();
    this.setData({
      ["message.name"]: event.detail.value,
    })
    app.userInfo.name = event.detail.value;
  },
  changePhone(event) {
    var app = getApp();
    this.setData({
      ["message.phone"]: event.detail.value,
    })
    app.userInfo.phone = event.detail.value;
  },
  changeStudentId(event) {
    var app = getApp()
    this.setData({
      ["message.studentId"]: event.detail.value,
    })
    app.userInfo.id = event.detail.value
  },


  onConfirm_School(event) {
    const { picker, value, index } = event.detail;
    wx.redirectTo({
      url: '/pages/message/index',
      success: (result) => {
        var app = getApp();
        app.userInfo.schoolName = this.data.School_Name_Gather[index]
      },
    });
  },
  onConfirm_Department(event) {
    const { picker, value, index } = event.detail;
    wx.redirectTo({
      url: '/pages/message/index',
      success: (result) => {
        var app = getApp();
        app.userInfo.deptName = this.data.Department_Name_Gather[index]
      }, fail: (err) => {
        console.log(err)
      },
    });
  },
  onConfirm_Class(event) {
    const { picker, value, index } = event.detail;
    var app = getApp();
    app.userInfo.className = this.data.Class_Name_Gather[index];
    this.data.Class_Message_Gather.forEach(item => {
      if (item.name == this.data.Class_Name_Gather[index]) {
        app.userInfo.classId = item.id;
      }
    })
    wx.redirectTo({
      url: '/pages/message/index',
      success: (result) => {

      },
    });
  },
  onCancel() {
    // Toast('取消');
    wx.redirectTo({
      url: '/pages/message/index',
      success: (result) => {

      },
    });
  },

})

